/**
 * 配置人群策略
 */
exports.saveStrategy = async context => {
  try {
    const data = context.data;
    const response = await context.cloud.topApi.invoke({
      api: "taobao.opencrm.crowd.strategy.write",
      data: {
        crowd_strategy: {
          details: {
            content: data.content, // 人群命中时的物料
            crowd_id: data.crowd_id, // 人群id
            gmt_end: data.gmt_end, // 策略结束时间
            gmt_start: data.gmt_start, // 策略开始时间
            name: data.ruleName, // 规则名称
            priority: 0, // 优先级,
            strategy_desc: "" // 规则描述
          },
          id: data.strategyId ? data.strategyId : 0, // id, 0为新增, 非0为修改
          strategy_desc: "", // 策略描述
          strategy_name: "" // 策略名称
        }
      }
      // autoSession: true,
    });
    if (response.is_success) {
      return { success: true, data: response.result };
    }
    return { success: true, data: data };
  } catch (e) {
    return { success: false, data: e };
  }
};

/**
 * 查询人群策略配置
 */
exports.queryStrategy = async context => {
  try {
    const response = await context.cloud.topApi.invoke({
      api: "taobao.opencrm.crowd.strategy.query",
      data: {}
    });
    return response;
  } catch (e) {
    return { success: false, data: e };
  }
};

exports.crowdsQuery = async context => {
  const data = context.data;
  const response = await context.cloud.topApi.invoke({
    api: "taobao.opencrm.crowds.query",
    data: {
      page: data.page,
      size: data.size
    },
    autoSession: true
  });
  return { success: true, data: response };
};

exports.systemQuery = async context => {
  const response = await context.cloud.topApi.invoke({
    api: "taobao.opencrm.crowds.system.query",
    data: {},
    autoSession: true
  });
  return { success: true, data: response };
};
